from shpReader import *

class CoordTransformation():
    def __init__(self,bsPath):
        self.bsShp = SHPReader(bsPath)
        self.scaleX = (self.bsShp.shpMaxX-self.bsShp.shpMinX)
        self.scaleY = (self.bsShp.shpMaxY-self.bsShp.shpMinY)
        self.minX=self.bsShp.shpMinX
        self.maxY=self.bsShp.shpMaxY

    def transformBSCoords(self):       
        transBSCoordDict={}
        
        for key, value in self.bsShp.shpDict.items():
            transBSCoord=[]
            j=0
            while j<len(value):
                transBSCoord.append(((value[j][0]-self.minX+2000)*900)/self.scaleX)
                transBSCoord.append(((self.maxY-value[j][1]+2000)*600)/self.scaleY)
                j=j+1
                
            transBSCoordDict[key]=transBSCoord
            
        return transBSCoordDict
    
    def transformMVCoords(self, mvPath):  
        mvShp = SHPReader(mvPath)
        transMVCoordDict={}
        
        for key, value in mvShp.shpDict.items():
            transMVCoord=[]
            initialCoord=[]
            j=0
            while j<len(value):
                initialCoord.append(value[j][0])
                initialCoord.append(value[j][1])
                transMVCoord.append(((value[j][0]-self.minX+2000)*900)/self.scaleX)
                transMVCoord.append(((self.maxY-value[j][1]+2000)*600)/self.scaleY)
                j=j+1
                
            transMVCoordDict[key]=[initialCoord,transMVCoord]
            
        return [transMVCoordDict, mvShp.dbfDict, mvShp.fieldNames]
    
    def transformInterval(self, interval):
        return (((interval*900)/self.scaleX)**2+((interval*900)/self.scaleY)**2)**(0.5)
    
    def transformSFCoords(self, sfCoorList):
        transSFCoor=[]
        for cl in sfCoorList:
            transSFCoor.append(((cl[0]-self.minX+2000)*900)/self.scaleX)
            transSFCoor.append(((self.maxY-cl[1]+2000)*600)/self.scaleY)
            
        return transSFCoor